package com.originskyseed.platform.recruit.service.impl;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.stereotype.Component;

import com.originskyseed.platform.core.base.BasePage;
import com.originskyseed.platform.core.util.bean.PrimaryKeyGeneratorUtil;
import com.originskyseed.platform.recruit.mapper.RecruitMapper;
import com.originskyseed.platform.recruit.model.RankingListVO;
import com.originskyseed.platform.recruit.model.Recruit;
import com.originskyseed.platform.recruit.service.IRecruitService;

/**
 * 招聘信息表Service接口实现类（OSI_RECRUIT）
 *
 * @author Tangwanghui
 */
@DubboService
@Component("recruitService")
public class RecruitServiceImpl implements IRecruitService {

	@Resource
	private RecruitMapper recruitMapper;

	/**
	 * 删除记录
	 *
	 * @param id
	 * @return
	 * @throws Exception
	 */
	@Override
	public int deleteByPrimaryKey(String id) throws Exception {
		return this.recruitMapper.deleteByPrimaryKey(id);
	}

	/**
	 * 批量删除记录
	 *
	 * @param ids
	 * @return
	 * @throws Exception
	 */
	@Override
	public int deleteByPrimaryKeys(List<String> ids) throws Exception {
		return this.recruitMapper.deleteByPrimaryKeys(ids);
	}

	/**
	 * 插入记录
	 *
	 * @param record
	 * @return
	 * @throws Exception
	 */
	@Override
	public int insert(Recruit record) throws Exception {
		if (StringUtils.isBlank(record.getId())) {
			record.setId(PrimaryKeyGeneratorUtil.getInstance().getRandomKey());
		}
		record.setCreateTime(new Date());
		return this.recruitMapper.insert(record);
	}

	/**
	 * 插入记录（仅非空值字段）
	 *
	 * @param record
	 * @return
	 * @throws Exception
	 */
	@Override
	public int insertSelective(Recruit record) throws Exception {
		if (StringUtils.isBlank(record.getId())) {
			record.setId(PrimaryKeyGeneratorUtil.getInstance().getRandomKey());
		}
		record.setCreateTime(new Date());
		return this.recruitMapper.insertSelective(record);
	}

	/**
	 * 更新记录
	 *
	 * @param record
	 * @return
	 * @throws Exception
	 */
	@Override
	public int updateByPrimaryKey(Recruit record) throws Exception {
		record.setUpdateTime(new Date());
		return this.recruitMapper.updateByPrimaryKey(record);
	}

	/**
	 * 更新记录（仅非空值字段）
	 *
	 * @param record
	 * @return
	 * @throws Exception
	 */
	@Override
	public int updateByPrimaryKeySelective(Recruit record) throws Exception {
		record.setUpdateTime(new Date());
		return this.recruitMapper.updateByPrimaryKeySelective(record);
	}

	/**
	 * 查询记录
	 *
	 * @param id
	 * @return
	 * @throws Exception
	 */
	@Override
	public Recruit selectByPrimaryKey(String id) throws Exception {
		return this.recruitMapper.selectByPrimaryKey(id);
	}

	/**
	 * 查询列表
	 *
	 * @param record
	 * @return
	 * @throws Exception
	 */
	@Override
	public List<Recruit> selectList(Recruit record) throws Exception {
		return this.recruitMapper.selectList(record);
	}

	/**
	 * 分页查询列表
	 *
	 * @param params
	 * @param page
	 * @return
	 * @throws Exception
	 */
	@Override
	public BasePage<Recruit> selectListByPage(Map<String, Object> params, BasePage<Recruit> page) throws Exception {
		params.put("page", page);
		List<Recruit> list = this.recruitMapper.selectListByPage(params);
		page.setResults(list);
		return page;
	}

	/**
	 * 查询模板是否使用
	 *
	 * @param recruitTemplateId
	 * @return
	 * @throws Exception
	 */
	public Boolean selectRecruitTemplateId(String recruitTemplateId) throws Exception {
		return this.recruitMapper.selectRecruitTemplateId(recruitTemplateId);
	}

	/**
	 * 批量修改状态
	 *
	 * @param map
	 * @return
	 * @throws Exception
	 */
	@Override
	public int updateStatusByIds(Map<String, Object> map) throws Exception {
		return this.recruitMapper.updateStatusByIds(map);
	}

	/**
	 * 查询推荐榜单
	 * 
	 * @param map
	 * @return
	 * @throws Exception
	 */
	@Override
	public List<RankingListVO> selectRankingList(Map<String, Object> map) throws Exception {
		return this.recruitMapper.selectRankingList(map);
	}

	/**
	 *
	 * @param params
	 * @param page
	 * @return
	 * @throws Exception
	 */
	@Override
	public BasePage<RankingListVO> selectRankingListByPage(Map<String, Object> params, BasePage<RankingListVO> page) throws Exception {
		params.put("page", page);
		List<RankingListVO> list = this.recruitMapper.selectRankingListByPage(params);
		page.setResults(list);
		return page;
	}

	/**
	 * 查询推荐人数
	 * 
	 * @param map
	 * @return
	 * @throws Exception
	 */
	@Override
	public Integer selectRecommendCount(Map<String, Object> map) throws Exception {
		return this.recruitMapper.selectRecommendCount(map);
	}

	/**
	 * 查询内荐费用
	 *
	 * @param map
	 * @return
	 * @throws Exception
	 */
	public BigDecimal selectRecommendAmount(Map<String, Object> map) throws Exception{
		return this.recruitMapper.selectRecommendAmount(map);
	}

	/**
	 * 查询排行第几
	 * 
	 * @param map
	 * @return
	 * @throws Exception
	 */
	public Integer selectRankingByCount(Map<String, Object> map) throws Exception {
		String userId = (String) map.get("userId");
		Integer rank = null;
		List<RankingListVO> list = this.recruitMapper.selectGreaterThanZeroRankingList(map);
		for (RankingListVO rankingListVO : list) {
			if (rankingListVO.getId().equals(userId)) {
				rank = rankingListVO.getRank();
			}
		}
		if (rank == null) {
			rank = list.size() + 1;
		}
		return rank;
	}
}
